import { api } from './api'
import type { SSLCertificateStatus, ApiResponse, ApiData } from '../types';

interface UploadCertificateData {
  name: string;
  cert_content: string;
  key_content: string;
  expiry_date: string;
}

export interface SSLCertInfo {
  domain: string;
  cert_path: string;
  key_path: string;
  server_name: string;
  listen_port: number;
}

export interface SSLCertificate {
  domain: string;
  cert_path: string;
  key_path: string;
  server_name: string[];
  listen_port: number;
  ssl_protocols?: string;
  ssl_ciphers?: string;
  ssl_prefer_server_ciphers?: boolean;
  ssl_session_timeout?: string;
  ssl_session_cache?: string;
  ssl_stapling?: boolean;
  ssl_stapling_verify?: boolean;
  ssl_trusted_certificate?: string;
  status: 'valid' | 'expired' | 'error' | 'unknown';
  not_before: string;
  not_after: string;
  issuer: string;
  type: 'manual' | 'auto';
  serialNumber?: string;
  signatureAlgorithm?: string;
  fingerprint?: string;
}

export interface AddCertificateDTO {
  domain: string;
  cert: File;
  key: File;
}

export const sslService = {
  // 获取所有证书
  async getCertificates(): Promise<SSLCertificate[]> {
    const response = await api.get<SSLCertificate[]>('/ssl/certificates');
    return response.data;
  },

  // 添加新证书
  async addCertificate(certData: AddCertificateDTO): Promise<void> {
    const formData = new FormData();
    formData.append('domain', certData.domain);
    formData.append('cert', certData.cert);
    formData.append('key', certData.key);
    
    await api.post('/ssl/certificates', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  },

  // 删除证书
  async deleteCertificate(domain: string): Promise<void> {
    await api.delete(`/ssl/certificates/${encodeURIComponent(domain)}`);
  },

  // 下载证书
  async downloadCertificate(domain: string): Promise<Blob> {
    const response = await api.get<Blob>(`/ssl/certificates/${encodeURIComponent(domain)}/download`, {
      responseType: 'blob'
    });
    return response.data;
  },

  async getAllCertificates(): Promise<SSLCertificate[]> {
    const response = await api.get<ApiResponse<SSLCertificate[]>>('/ssl/certificates');
    const result = response as unknown as ApiData<SSLCertificate[]>;
    return result.data;
  },

  async getCertificate(id: number): Promise<SSLCertificate> {
    const response = await api.get<ApiResponse<SSLCertificate>>(`/ssl/certificates/${id}`);
    const result = response as unknown as ApiData<SSLCertificate>;
    return result.data;
  },

  async uploadCertificate(data: UploadCertificateData): Promise<SSLCertificate> {
    const response = await api.post<ApiResponse<SSLCertificate>>('/ssl/certificates', data);
    const result = response as unknown as ApiData<SSLCertificate>;
    return result.data;
  },

  async checkExpiry(): Promise<SSLCertificateStatus[]> {
    const response = await api.get<ApiResponse<SSLCertificateStatus[]>>('/ssl/check-expiry');
    const result = response as unknown as ApiData<SSLCertificateStatus[]>;
    return result.data;
  },

  // 测试配置
  async testConfig(): Promise<{ message: string }> {
    const response = await api.post<{ message: string }>('/ssl/test');
    return response.data;
  },

  // 重新加载配置
  async reloadConfig(): Promise<{ message: string }> {
    const response = await api.post<{ message: string }>('/ssl/reload');
    return response.data;
  }
}; 